package com.shuosen.gmall.cart.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.shuosen.gmall.bean.CartInfo;
import com.shuosen.gmall.bean.SkuInfo;
import com.shuosen.gmall.config.CookieUtil;
import com.shuosen.gmall.service.ManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@Component
public class CartCookieHandler {

    // 定义购物车名称
    private String cookieCartName = "CART";
    // 设置cookie 过期时间
    private int COOKIE_CART_MAXAGE=7*24*3600;


    @Reference
    ManageService manageService  ;

    public void addToCart(HttpServletRequest request, HttpServletResponse response, String skuId, String userId, int skuNum) {
        /***
         *1：添加的时候，如果购物车中没有修改商品，则直接添加
         * 2：如果购物车中有该商品数量相加
         */
        //获取cookie中的购物车数据 中文所以设置编码为
        //token 不需要编码 因为是字符串
        List<CartInfo> cartInfoList = new ArrayList<>();
        String cookieValue = CookieUtil.getCookieValue(request, cookieCartName, true);

        boolean ifExists = false  ;
        if(StringUtils.isNotEmpty(cookieValue)){
            //cookievalue 将字符串 转换成cartInfo对象
            //转换为集合对象用JSON的parseArray
            cartInfoList = JSON.parseArray(cookieValue, CartInfo.class);
            for (CartInfo cartInfo : cartInfoList) {
                //循环cartInfoList ，添加的SkuId是否在集合中
                if (cartInfo.getSkuId().equals(skuId)) {
                    //数量相加
                    cartInfo.setSkuNum(cartInfo.getSkuNum()+skuNum);
                    //第一次添加的商品放到集合里面
                    //给skuPrice赋值
                    cartInfo.setSkuPrice(cartInfo.getCartPrice());
                    ifExists = true ;
                }
            }
        }
        //说明购物车中没有商品  第一次添加
        if(!ifExists){
            //不存在商品
            SkuInfo skuInfo = manageService.getSkuInfo(skuId);
            CartInfo cartInfo=new CartInfo();

            cartInfo.setSkuId(skuId);
            cartInfo.setCartPrice(skuInfo.getPrice());
            cartInfo.setSkuPrice(skuInfo.getPrice());
            cartInfo.setSkuName(skuInfo.getSkuName());
            cartInfo.setImgUrl(skuInfo.getSkuDefaultImg());

            cartInfo.setUserId(userId);
            cartInfo.setSkuNum(skuNum);
            cartInfoList.add(cartInfo);
        }
        //将集合放到cookie中
        String newCartInfo = JSON.toJSONString(cartInfoList);
        CookieUtil.setCookie(request,response,cookieCartName,newCartInfo,COOKIE_CART_MAXAGE,true);
    }

    public List<CartInfo> getCartList(HttpServletRequest request) {
        String cartJson = CookieUtil.getCookieValue(request, cookieCartName, true);
        List<CartInfo> cartInfoList = JSON.parseArray(cartJson, CartInfo.class);
        return cartInfoList ;
    }

    public void deleteCartCooke(HttpServletRequest request, HttpServletResponse response) {
            CookieUtil.deleteCookie(request,response,cookieCartName);
    }

    public void checkCart(HttpServletRequest request, HttpServletResponse response, String skuId, String isChecked) {
        //取出购物车的商品 
        List<CartInfo> cartList = getCartList(request);
        for (CartInfo cartInfo : cartList) {
            if(cartInfo.getSkuId().equals(skuId)){
                cartInfo.setIsChecked(isChecked);

            }
        }

        //保存到cookie
        String newCartJson = JSON.toJSONString(cartList);
        CookieUtil.setCookie(request,response,cookieCartName,newCartJson,COOKIE_CART_MAXAGE,true);

    }
}
